package com.tlias.Service.impl;

import com.tlias.Service.DeptLogService;
import com.tlias.Service.DeptService;
import com.tlias.mapper.DeptMapper;
import com.tlias.mapper.EmpMapper;
import com.tlias.pojo.Dept;
import com.tlias.pojo.DeptLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    //注入Mepper接口
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptLogService deptLogService;

    /*
     * 查询全部部门数据
     * */
    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }

    /*
     * 删除部门数据
     * */
    @Transactional(rollbackFor = Exception.class)//spring事物管理
    @Override
    public void deleteByid(Integer id) throws Exception {
        try {
            deptMapper.deleteById(id);//根据部门id删除部门

            //if (true){ throw new Exception("出错啦");}
            int i = 1/0;
            empMapper.deleteByDeptId(id); //根据部门id删除部门下的员工

        } finally {
            DeptLog deptLog = new DeptLog();
            deptLog.setCreateTime(LocalDateTime.now());
            deptLog.setDescription("执行了部门解散的操作，解散的部门是"+id+"号部门");
            deptLogService.insert(deptLog);
        }
    }

    /*
     * 新增部门
     * */
    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.add(dept);

    }

    /*
     * 根据ID查询数据
     * */
    @Override
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }

    /*
     * 修改部门数据
     * */
    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }

    /*
     * 修改部门数据
     * */
   /* @Override
    public List<Dept> update(Integer id, String name) {
        LocalDateTime updateTime = LocalDateTime.now();
        deptMapper.update(id,name,updateTime);

        //调用查询方法，回显更改后的数据

        //将数据封装到Dept类中返回Controller层
        return deptMapper.getById(id);
    }*/


}
